<template>
  <div>
    <div class="home">
      <div class="container">
        <ul class="mid-list">
          <li class="menu-item">
            <a href="#">手机 <i class="icon-right"></i></a>
            <div class="children">
              <ul v-for="(item, i) in menuList" :key="i">
                <li v-for="subitem in item" :key="subitem.id">
                  <a :href="`/product/${subitem.id}`" target="_blank">
                    <img
                      :src="subitem ? subitem.img : '/imgs/item-box-1.png'"
                      alt=""
                      class="pro-img"
                    />

                    <span class="pro-name">
                      {{ subitem ? subitem.name : "小米9" }}
                    </span>
                  </a>
                </li>
              </ul>
            </div>
          </li>

          <li class="menu-item">
            <a href="#">电视 <i class="icon-right"></i></a>
          </li>
          <li class="menu-item">
            <a href="#">笔记本 平板 <i class="icon-right"></i></a>
          </li>

          <li class="menu-item">
            <a href="#">家电 <i class="icon-right"></i></a>
          </li>
          <li class="menu-item">
            <a href="#">出行 穿戴 <i class="icon-right"></i></a>
          </li>
          <li class="menu-item">
            <a href="#">智能 路由器 <i class="icon-right"></i></a>
          </li>

          <li class="menu-item">
            <a href="#">电源 配件 <i class="icon-right"></i></a>
          </li>
          <li class="menu-item">
            <a href="#">健康 儿童<i class="icon-right"></i></a>
          </li>
          <li class="menu-item">
            <a href="#">耳机 音箱<i class="icon-right"></i></a>
          </li>
          <li class="menu-item">
            <a href="#">生活 箱包<i class="icon-right"></i></a>
          </li>
        </ul>

        <Carousel :list="bannerList" />
        <div class="ad-container" clearfix>
          <div class="span4">
            <ul class="home-channel-list clearfix">
              <li>
                <a
                  target="_blank"
                  href="https://api.jr.mi.com/activity/scene/scenePCsearch.html?from=search"
                  ><img
                    src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/82abdba456e8caaea5848a0cddce03db.png?w=48&amp;h=48"
                    alt="保障服务"
                  />
                  保障服务
                </a>
              </li>
              <li>
                <a target="_blank" href="https://qiye.mi.com/"
                  ><img
                    src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/806f2dfb2d27978e33fe3815d3851fa3.png?w=48&amp;h=48"
                    alt="企业团购"
                  />
                  企业团购
                </a>
              </li>
              <li>
                <a target="_blank" href="https://www.mi.com/order/fcode"
                  ><img
                    src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/eded6fa3b897a058163e2485532c4f10.png?w=48&amp;h=48"
                    alt="F码通道"
                  />
                  F码通道
                </a>
              </li>
              <li>
                <a target="_blank" href="https://10046.mi.com"
                  ><img
                    src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/43a3195efa6a3cc7662efed8e7abe8bf.png?w=48&amp;h=48"
                    alt="米粉卡"
                  />
                  米粉卡
                </a>
              </li>
              <li>
                <a target="_blank" href="//www.mi.com/a/h/16769.html"
                  ><img
                    src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/f4846bca6010a0deb9f85464409862af.png?w=48&amp;h=48"
                    alt="以旧换新"
                  />
                  以旧换新
                </a>
              </li>
              <li>
                <a target="_blank" href="https://recharge.10046.mi.com/"
                  ><img
                    src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/9a76d7636b08e0988efb4fc384ae497b.png?w=48&amp;h=48"
                    alt="话费充值"
                  />
                  话费充值
                </a>
              </li>
            </ul>
          </div>
          <div class="ad-box">
            <a
              v-for="item in adList"
              :key="item.id"
              :href="`/product/${item.id}`"
            >
              <img v-lazy="item.img" alt="" />
            </a>
          </div>
        </div>
        <div class="banner">
          <a href="/product/30">
            <img v-lazy="'/imgs/banner-1.png'" alt="" />
          </a>
        </div>
      </div>
      <div class="phone">
        <div class="container">
          <h2>手机</h2>
          <div class="wrapper">
            <div class="phone-left">
              <router-link to="/product/35"
                ><img src="/imgs/mix-alpha.jpg" alt=""
              /></router-link>
            </div>
            <div class="phone-list">
              <ul v-for="(arr, index) in phoneList" :key="index">
                <li v-for="(sub, index) in arr" :key="sub.id">
                  <span v-if="index % 2 == 1" class="kill-pro">秒杀</span>
                  <span v-else class="new-pro">新品</span>

                  <a href="" :title="sub.name">
                    <img :src="sub.mainImage" alt=""
                  /></a>
                  <div class="item-info">
                    <h3>{{ sub.name }}</h3>
                    <p>{{ sub.subtitle }}</p>
                    <p class="price" @click="addCart(sub.id)">
                      {{ sub.price }}元
                      <span></span>
                    </p>
                  </div>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>

      <Modal
        title="提示"
        sureText="查看购物车"
        btnType="1"
        modalType="middle"
        v-show="showModal"
        @close="showModal = false"
        @submit="gotoCart"
      >
        <template slot="body">
          <p>商品添加成！</p>
        </template>
      </Modal>
    </div>
    <BarService />
    <RightFixed />
  </div>
</template>

<script>
import mockRequests from "@/api/mockAjax";
import requests from "@/api/ajax";

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "HomeIndex",

  data() {
    return {
      bannerList: [],
      menuList: [],
      adList: [],
      phoneList: [],
      showModal: false,
    };
  },

  mounted() {
    mockRequests.get("/banner").then((res) => {
      console.log(res);
      this.bannerList = res.data;
    }),
      mockRequests.get("/menu").then((res) => {
        console.log(res);
        this.menuList = res.data;
      });
    mockRequests.get("/ad").then((res) => {
      console.log(res);
      this.adList = res.data;
    });
    requests
      .get("/products", {
        params: {
          categoryId: 100012,
          pageSize: 14,
        },
      })
      .then((res) => {
        res.data.list = res.data.list.slice(6, 14);
        this.phoneList = [res.data.list.slice(0, 4), res.data.list.slice(4, 8)];
      });
  },

  methods: {
    gotoCart() {
      this.$router.push("/cart");
    },
    addCart(id) {
      requests
        .post("/carts", {
          productId: id,
          selected: true,
        })
        .then((res) => {
          this.$store.dispatch("saveCartCount", res.data.cartTotalQuantity);
          // this.$router.push('/cart');
          this.showModal = true;
        })
        .catch((this.showModal = false));
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../../assets/scss/mixin";
@import "../../../assets/scss/config";
.container > div,
.container ~ div {
  margin-top: 50px;
}
.mid-list {
  position: absolute;
  z-index: 10;
  width: 234px;
  height: 451px;
  background-color: #55585a7a;
  padding: 16px 0;
  opacity: 0.9;
  .menu-item {
    line-height: 42px;
    height: 42px;
    &:hover {
      background: $colorA;
      .children {
        width: 992px;
        display: block;
      }
    }
    .children {
      display: none;
      width: 992px;
      transition: all 0.5s;

      height: 451px;
      background-color: #fff;
      position: absolute;
      top: 0;
      left: 234px;
      border: 1px solid $colorH;
      // 用弹性和布局时，display：flex可以和display：none一起使用吗
      // display: flex;
      // flex-direction: column;

      text-align: center;

      ul {
        width: 100%;
        flex: 1;
        height: 75px;
        @include flex() // justify-content:space-between;
          li {
          // border: 1px solid red;
          flex: 1;
        }
        a {
          display: flex;
          flex-direction: column;
          align-items: center;

          // width: 100%;
          // height: 100%;
          // border: 1px solid red;
          color: $colorB;
          font-size: 14px;
          img {
            width: 42px;
            height: 35px;
            vertical-align: middle;
          }
        }
      }
    }
    a {
      // 给icon定位
      position: relative;
      padding-left: 30px;
      // 点击超链接
      display: block;

      font-size: 16px;
      color: #fff;
    }
  }

  .icon-right {
    position: absolute;
    right: 10px;
    top: 12px;
    @include bgImg(10px, 15px, "../../../../public/imgs/icon-arrow.png");
  }
}

.swiper-container {
  height: 451px;
  position: relative;
}
.ad-container {
  height: 167px;
  .span4,
  .ad-box,
  li {
    float: left;
  }
  .span4 {
    width: 234px;
    height: 167px;
    background-color: #5f5750;
    background-color: #5f5750;

    li {
      position: relative;
      float: left;
      height: 83.5px;
      width: 78px;
      &::before {
        content: "";
        position: absolute;
        left: 0;
        right: 0;
        margin: 0 auto;
        //水平居中
        top: 0;

        width: 64px;
        height: 1px;

        background: #665e57;
      }
      &::after {
        content: "";
        position: absolute;
        height: 70px;
        width: 1px;
        background-color: #665e57;
        top: 0;
        bottom: 0;
        margin: auto 0;
        left: 0;
      }

      // border: 1px solid rgb(219, 183, 183);
    }
    a {
      width: 100%;
      height: 100%;

      display: block;
      font-size: 12px;
      color: #cfccca;
      text-align: center;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: space-around;
      img {
        width: 24px;
        height: 24px;
      }
      &:hover {
        color: #fff;
      }

      /* 开启bfc的体验素可以解决外边距重叠 */
    }
  }

  .ad-box {
    width: 982px;

    margin-left: 10px;
    @include flex() a {
      width: 320px;
      height: 167px;
    }
  }
}

.phone {
  background-color: $colorJ;
  padding: 30px 0 50px;
  h2 {
    font-size: $fontF;
    height: 21px;
    line-height: 21px;
    color: $colorB;
    margin-bottom: 20px;
  }
  .wrapper {
    height: 620px;
    display: flex;
  }
}
.phone-left {
  margin-right: 16px;
  &:hover {
    box-shadow: 2px 015px #e5e5e5;
    transform: translateY(-2px);
  }
  a {
    img {
      width: 224px;
      height: 610px;
    }
  }
}
.phone-list {
  text-align: center;
  height: 100%;

  span {
    display: inline-block;
    width: 67px;
    height: 24px;
    line-height: 24px;
    font-size: 14px;

    color: $colorG;
    &.new-pro {
      background-color: #7ecf68;
    }
    &.kill-pro {
      background-color: #e82626;
    }
  }

  ul {
    height: 295px;
    margin-bottom: 20px;
    @include flex();
    &:last-child {
      margin-bottom: 0;
    }
    li {
      width: 100%;
      height: 100%;
      background-color: $colorG;
      margin: 0 10px;
      transition: all 0.2s linear;
      &:hover {
        box-shadow: 0 15px 30px #e5e5e5;
        -webkit-transform: translate3d(0, -2px, 0);
        transform: translate3d(0, -2px, 0);
        // box-shadow: 0px 5px 25px #e5e5e5;
        // transform: translateY(-2px);
      }
    }
    a {
      padding: 10px;
      display: block;
      img {
        width: 80%;
        height: 80%;
      }
    }
  }
  .item-info {
    margin-top: 14px;
    text-align: center;
    h3 {
      font-size: $fontJ;
      color: $colorB;
      line-height: $fontJ;
      font-weight: bold;
    }
    p {
      color: $colorD;
      line-height: 13px;
      margin: 6px auto 13px;
    }
    .price {
      color: #f20a0a;
      font-size: $fontJ;
      font-weight: bold;
      cursor: pointer;
      span {
        @include bgImg(
          22px,
          22px,
          "../../../../public/imgs/icon-cart-hover.png"
        );
        content: " ";

        margin-left: 5px;
        vertical-align: middle;
      }
    }
  }
}
</style>
